﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WindowsFormsOrderManagement
{
    public partial class DelForm : Form
    {
        public DelForm()
        {
            InitializeComponent();
           
        }
        private void label1_Click(object sender, EventArgs e)
        {
            //donothing
        }

        //确定删除：
        private async void button1_Click(object sender, EventArgs e)
        {
            string s1 = textBox1.Text;
            //检查是不是数字：
            if (int.TryParse(s1, out int input1))
            {//检查编号是否存在
                if (input1 <0 || input1 > initial.OrderID)
                {
                    DelErrorForm del1 = new DelErrorForm();
                    del1.ShowDialog();
                }
                //编号存在，开始删除：
                initial.orderService.RemoveOrder(s1);
                DelForm_Load_1(sender,e);
                label3.Text = "订单删除成功！";
                textBox1.Clear();
                await Task.Delay(2000);//等两秒
                label3.Text = "这里是信息提示";
            }
            else//出错弹窗
            { DelErrorForm del = new DelErrorForm();
                del.ShowDialog();
            }
        }
        //取消
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }

        //返回
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DelForm_Load_1(object sender, EventArgs e)
        {
            // 将 Order 列表转换为扁平化的匿名对象列表
            var data = initial.orderService.orders
                .Select(o => new
                {
                    OrderID = o.OrderId,
                    CustomerName = o.Customer.Name,
                    OrderDate = o.OrderDate,
                    ProductName = o.OrderDetails.Product.NAME,
                    Quantity = o.OrderDetails.Quantity,
                    Price = o.OrderDetails.Product.Price
                })
                .ToList();

            // 清空所有现有列（包括手动添加的）
            dataGridView1.Columns.Clear();

            // 允许自动生成列
            dataGridView1.AutoGenerateColumns = true;

            // 绑定到 DataGridView
            dataGridView1.DataSource = data;

            // 可选：调整列标题和格式
            dataGridView1.Columns["OrderID"].HeaderText = "订单号";
            dataGridView1.Columns["CustomerName"].HeaderText = "客户";
            dataGridView1.Columns["OrderDate"].HeaderText = "日期";
            dataGridView1.Columns["ProductName"].HeaderText = "商品";
            dataGridView1.Columns["Quantity"].HeaderText = "数量";
            dataGridView1.Columns["Price"].HeaderText = "单价";
            dataGridView1.Columns["OrderDate"].DefaultCellStyle.Format = "yyyy-MM-dd";
        }
    }
}
